//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package flc.ast.view;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import easily.tech.guideview.lib.GuideViewBundle;
import easily.tech.guideview.lib.R.layout;
import easily.tech.guideview.lib.R.style;
import flc.ast.R;

import java.util.ArrayList;
import java.util.List;

public class GuideViewFragment extends DialogFragment {
    private List<GuideViewBundle> guideViewBundles = new ArrayList();
    private FrameLayout flContainer;
    private GuideViewBundle currentBundle;
    private GuideView currentGuideView;
    private ImageView ivDelete, ivRename;

    public GuideViewFragment() {
    }

    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setStyle(DialogFragment.STYLE_NO_TITLE, style.GuideViewDialogStyle);
    }

    @Nullable
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_guide_container, container, false);
//        this.flContainer = (FrameLayout) inflater.inflate(R.layout.layout_guide_container, container, false);
        this.flContainer = view.findViewById(R.id.fl_container);
        ivDelete = view.findViewById(R.id.ivDelete);
        ivRename = view.findViewById(R.id.ivRename);
        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.clickDelete();
            }
        });
        ivRename.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.clickRename();
            }
        });

        return view;
    }

    public void onStart() {
        super.onStart();
        Dialog dialog = this.getDialog();
        Window window = dialog == null ? null : dialog.getWindow();
        if (window != null) {
            window.setLayout(-1, -1);
            window.setBackgroundDrawable(new ColorDrawable(0));
            this.showGuideView();
        }
    }

    public void setGuideViewBundles(List<GuideViewBundle> guideViewBundles) {
        if (guideViewBundles != null && !guideViewBundles.isEmpty()) {
            this.guideViewBundles = guideViewBundles;
        }
    }

    public void onNext() {
        this.showGuideView();
    }

    public boolean hasNext() {
        return this.guideViewBundles != null && !this.guideViewBundles.isEmpty();
    }

    private void showGuideView() {
        if (this.currentGuideView != null && this.currentGuideView.isShowing) {
            this.flContainer.setBackgroundColor(this.currentBundle == null ? 0 : this.currentBundle.getMaskColor());
            this.currentGuideView.hide();
        }

        if (this.guideViewBundles != null && !this.guideViewBundles.isEmpty()) {
            this.currentBundle = (GuideViewBundle) this.guideViewBundles.remove(0);
            if (this.currentBundle == null) {
                this.dismiss();
            } else {
                GuideView guideView = new GuideView(this.getContext(), this.currentBundle);
                this.wrapClickListener(guideView);
                this.flContainer.addView(guideView);
                guideView.show();
                this.currentGuideView = guideView;
            }
        } else {
            this.dismiss();
        }
    }

    private void wrapClickListener(View guideView) {
        if (this.currentBundle != null && this.currentBundle.isDismissOnClicked()) {
            guideView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    listener.clickCancel();
                    GuideViewFragment.this.onNext();
                }
            });
        }
    }

    public void dismiss() {
        if (this.getContext() instanceof Activity && !((Activity) this.getContext()).isFinishing() && this.getDialog() != null && this.getDialog().isShowing()) {
            if (this.flContainer != null) {
                this.flContainer.removeAllViews();
                this.currentBundle = null;
                this.currentGuideView = null;
            }

            super.dismiss();
        }

    }

    public static class Builder {
        private List<GuideViewBundle> guideViewBundles = new ArrayList();
        private boolean cancelable;

        public Builder() {
        }

        public Builder addGuidViewBundle(GuideViewBundle bundle) {
            if (bundle == null) {
                return this;
            } else {
                this.guideViewBundles.add(bundle);
                return this;
            }
        }

        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public GuideViewFragment build() {
            GuideViewFragment fragment = new GuideViewFragment();
            fragment.setGuideViewBundles(this.guideViewBundles);
            fragment.setCancelable(this.cancelable);
            return fragment;
        }
    }

    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void clickCancel();

        void clickDelete();

        void clickRename();
    }

}
